"""
PostgreSQL 数据库连接器
"""
import psycopg2
from typing import Dict, Any
from .base_connector import BaseConnector


class PostgreSQLConnector(BaseConnector):
    """
    PostgreSQL 数据库连接器
    """
    def __init__(self):
        self.connection = None

    def connect(self, connection_params: Dict[str, Any]) -> None:
        """
        连接 PostgreSQL 数据库
        
        Args:
            connection_params: 连接参数
        """
        import os
        self.connection = psycopg2.connect(
            host=os.getenv("DB_HOST", "localhost"),
            user=os.getenv("DB_USER", "postgres"),
            password=os.getenv("DB_PASSWORD", ""),
            database=os.getenv("DB_NAME", "test_db")
        )

    def execute_query(self, query: str) -> Dict[str, Any]:
        """
        执行 SQL 查询
        
        Args:
            query: SQL 查询语句
        """
        cursor = self.connection.cursor()
        cursor.execute(query)
        columns = [desc[0] for desc in cursor.description]
        result = [dict(zip(columns, row)) for row in cursor.fetchall()]
        cursor.close()
        return result

    def close(self) -> None:
        """
        关闭数据库连接
        """
        if self.connection:
            self.connection.close()